﻿using System;
using System.IO;
using System.Runtime.Serialization;

namespace _040_序列化数据协定时忽略某个成员
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream 执行序列化=new FileStream("rd.xml",FileMode.Create))
            {
                数据协定表 操作 = new 数据协定表
                {
                    RecordOrder = 1,
                    RecordTtitle = "Number",
                    SizeInBytes = 12105,
                    Tracked = true
                };
                DataContractSerializer 写入 = new DataContractSerializer(typeof(数据协定表));
                写入.WriteObject(执行序列化, 操作);
            }

            using (FileStream 反序列化= new FileStream("rd.xml",FileMode.Open))
            {
                DataContractSerializer 读出 = new DataContractSerializer(typeof(数据协定表));
                数据协定表 使用它 = 读出.ReadObject(反序列化) as 数据协定表;

                //输出值
                string msg = null;
                msg += $"{nameof(使用它.RecordOrder)}={使用它.RecordOrder}\n\r";
                msg += $"{nameof(使用它.RecordTtitle)}={使用它.RecordTtitle}\n\r";
                msg += $"{nameof(使用它.SizeInBytes)}={使用它.SizeInBytes}\n\r";
                msg += $"{nameof(使用它.Tracked)}={使用它.Tracked}\n\r";
                Console.WriteLine("反序列化得到的 数据协定表 实例：");
                Console.Write(msg);
            }

        }
    }

    [DataContract(Namespace ="test - rd - data",Name ="rd_body")]
    public class 数据协定表
    {
        [DataMember(Name ="rd_ord")]
        public int RecordOrder { get; set; }
        [DataMember(Name ="rd_title")]
        public string RecordTtitle { get; set; }
        [DataMember(Name ="rd_size")]
        public long SizeInBytes { get; set; }
        [IgnoreDataMember]
        public bool Tracked { get; set; }
    }


}
